package com.yxinmiracle.file;

import com.yxinmiracle.util.FastDFSClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @version 1.0
 * @author： YxinMiracle
 * @date： 2021-08-08 13:25
 */
@RestController
public class UploadController {

    @Value("${pic.url}")
    private String PicUrl;

    /**
     * 上传图片
     *
     * @param file
     * @return 图片链接
     */
    @PostMapping("/upload")
    public String upload(MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            String originalFilename = file.getOriginalFilename();
            String extName = StringUtils.getFilenameExtension(originalFilename);
            String[] upload = FastDFSClient.upload(new FastDFSFile(originalFilename, bytes, extName));
            String realPath = PicUrl + "/" + upload[0] + "/" + upload[1];
            return realPath;
        }
        return null;
    }
}
